
import sys
import shutil
import os

import pepe.p_filesystem.dfstats as pfd

def mvsha(f):
    '''
    Usage: mvsha FILE
    Calculate sha1 checksum and rename file with it. File extension is not altered.

    Report bugs to presovsky@gmail.com
    '''
    fs = pfd.dfstats(f, options=['sha1'], pprint=False)

    shasum = fs.values()[0]['sha1']

    fpath, fname = os.path.split(f)
    fbase, fext = os.path.splitext(f)

    if fpath == '':
        f_new = '%s%s' % (shasum, fext)
    else:
        f_new = '%s/%s%s' % (fpath, shasum, fext)
    shutil.move(f, f_new)

    return f_new

if __name__ == '__main__':
    if len(sys.argv[:]) > 1:
        print sys.argv[1]
        print 'Renamed: ', mvsha(sys.argv[1])
    else:
        print mvsha.__doc__



